package net.sf.xoperator.ui.txt;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.openbbs.blackboard.filter.KindOfFilter;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.ui.StringMessage;
import net.sf.xoperator.ui.aiml.AIMLTemplate;

public class RemoveAimlTemplate extends Command {

	private static final String DELELTE_TEMPLATE = "del template";

	@Override
	public String getHelp() {

		return "deletes a previously stored aiml template, usage: del template \"<pattern>\"";
	}

	@Override
	public String getKeyWord() {
		// TODO Auto-generated method stub
		return DELELTE_TEMPLATE;
	}

	@Override
	public List<Object> performInternal(String call, NoPlanBlackBoardControl control,
			Interactor interactor) {
		String pattern = call.split(" ")[2].toUpperCase();

		Set<?> aimltemps = control.getConfigurationContext().blackboard()
				.readAll(new KindOfFilter(AIMLTemplate.class));
		for (Object object : aimltemps) {
			AIMLTemplate atemp = (AIMLTemplate) object;
			if(atemp.getPattern().equals(pattern)){
				atemp.setObsolete(true);
				atemp.setPersistet(false);
				control.propagateConfiguration(atemp);
				return Arrays.asList(new Object[] { new StringMessage(
				"Template with pattern: " +atemp.getPattern() + " removed") });
			}
		}

		return Arrays.asList(new Object[] { new StringMessage(
				"Template not found") });
	}
	

	@Override
	public Pattern getValidationPattern() {

		return Pattern.compile(".*");
	}
}
